clear; close all; clc; 

load ../data/music_dataset.mat
addpath 'CV/' 'DT/' 'Lyrics_Kernel/' 'SVM/libsvm/'

[Xt_lyrics] = make_lyrics_sparse(train, vocab);
[Xq_lyrics] = make_lyrics_sparse(quiz, vocab);

Yt = zeros(numel(train), 1);
for i=1:numel(train)
    Yt(i) = genre_class(train(i).genre);
end

Xt_audio = make_audio(train);
Xq_audio = make_audio(quiz);


%% train on audio

nb = NaiveBayes.fit(Xt_audio,Yt,'Distribution','kernel');

%get test error on train set
probs = posterior(nb,Xt_audio);

re = rank_err(probs,Yt);

%% train on audio (CV on priors)

n = size(Xt_audio, 1);
n_folds = 10;

priors = rand(10,10);  %each row is a set of priors for the 10 classes
cv_errors = zeros(size(priors,1),1);
cv_stddev = zeros(size(priors,1),1);

for j=1:(size(priors,1)+1)

    fprintf('Param:  Prior # %d\n',j);
    
    %choose the number of partitions
    part = make_cv_partition(n, n_folds);

    % error vector 
    err = zeros(n_folds,1); 

    for i = 1:n_folds

        fprintf('Currently training on fold #%d ... \n', i); 

        %find the indicies of partitiion i
        train_ind = find(part~=i);
        test_ind = find(part==i);

        %Index the X and Y arrays by the appropriate indicies
        train_points = Xt_audio(train_ind,:); %Training set i (all indicies ~= i)
        train_labels = Yt(train_ind,:); %Training labels i (all indicies ~= i)
        ith_points = Xt_audio(test_ind,:); %Test set i (all indicies == i)
        ith_labels_true = Yt(test_ind); % Test labels i (all indices == i)

        %TRAIN
        if(j<size(priors,1)+1)
            nb = NaiveBayes.fit(train_points, train_labels,...
                'Distribution','kernel','Prior',priors(j,:));
        else %on last run, use built in prior calculation
            nb = NaiveBayes.fit(train_points, train_labels,...
                'Distribution','kernel');
        end
        
        %TEST (on ith fold)
        probs = posterior(nb,ith_points);

        %Call rank_err function
        err(i) = rank_err(probs, ith_labels_true);
    end

    cv_errors(j) = mean(err);
    cv_stddev(j) = std(err); 

end

figure(1);
set(gcf,'color','w');
set(gca,'fontsize',18);
hold on;
grid on;

labels = cell(size(priors,1)+1,1);

for j=1:size(priors,1)+1
    errorbar(j,cv_errors(j),cv_stddev(j),...
        'color',j/(size(priors,1)+1)*[0 1 0.5],'linewidth',2);
    if(j==size(priors,1)+1)
        labels{j} = 'MATLAB Default Prior';
    else
        labels{j} = ['Prior: ' num2str(priors(j,:))];
    end
end
set(gca,'xtick',[]);
title('Naive Bayes CV Error, Parameter: Prior');
ylabel('CV Rank Error');
legend(labels,'location','southoutside');









